/*
 * @Author: your name
 * @Date: 2021-11-12 21:33:25
 * @LastEditTime: 2021-12-04 21:46:43
 * @LastEditors: Please set LastEditors
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath: \li-yuwei\serve\app\service\article.js
 */
/*
 * @Author: your name
 * @Date: 2021-11-10 08:34:30
 * @LastEditTime: 2021-11-11 09:09:44
 * @LastEditors: Please set LastEditors
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath: \li-yuwei\egg-example\app\service\login.js
 */

const Service = require("egg").Service;
const timeFormart = require("../utils/timeFormart");
class CommentService extends Service {
    async getCommentList() {
            const { ctx, app } = this;
            const { pageSize, pageIndex, author, email, state } = ctx.query;
            let res = await app.mysql.select("comments");
            let total = res.length;
            // //搜索框搜索
            if (author) {
                res = res.filter((item) => {
                    if (
                        item.author.indexOf(author) !== -1 ||
                        item.author.includes(author)
                    ) {
                        return item;
                    }
                });
                total = res.length;
            }
            //状态搜索
            if (state !== "null") {
                res = res.filter((item) => item.state * 1 === state * 1);
                total = res.length;
            }
            //路径搜索
            if (email) {
                res = res.filter((item) => {
                    if (item.email.indexOf(email) !== -1 || item.email.includes(email)) {
                        return item;
                    }
                });
                total = res.length;
            }
            //时间倒序
            res.sort((a, b) => {
                return new Date(b.comment_time) - new Date(a.comment_time);
            });
            //分页
            res = res.slice((pageIndex - 1) * pageSize, pageSize * pageIndex);
            return { res, total };
        }
        //添加
    async addCommentList() {
            const { ctx, app } = this;
            const { email, content, article_id, parent_content, parent_id, author } =
            ctx.request.body;
            const res = await app.mysql.insert("comments", {
                ...ctx.request.body,
                comment_time: timeFormart(Date.now()),
            });
            return res;
        }
        //删除
    async delCommentList() {
            const { ctx, app } = this;
            const { id } = ctx.params;
            const res = await app.mysql.delete("comments", { id });
            return res;
        }
        //改变状态
    async changeCommentState() {
            const { ctx, app } = this;
            const { id } = ctx.request.body;
            const res = await app.mysql.get("comments", { id });
            const result = await app.mysql.update("comments", {
                id,
                state: !Number(res.state),
            });
            return result;
        }
        //通过
    async passComment() {
            const { ctx, app } = this;
            const { id } = ctx.request.body;
            const res = await app.mysql.update("comments", { id, state: 1 });
            return res;
        }
        //拒绝
    async refuseComment() {
            const { ctx, app } = this;
            const { id } = ctx.request.body;
            const res = await app.mysql.update("comments", { id, state: 0 });
            return res;
        }
        //批量删除
    async batchDelComments() {
            const { ctx, app } = this;
            const { batchList } = ctx.request.body;
            batchList.forEach((item) => {
                app.mysql.delete("comments", {
                    id: item.id,
                });
            });
            return "操作成功";
        }
        //批量通过
    async batchPassComments() {
            const { ctx, app } = this;
            const { batchList } = ctx.request.body;
            batchList.forEach((item) => {
                app.mysql.update("comments", {
                    id: item.id,
                    state: 1,
                });
            });
            return "操作成功";
        }
        //批量拒绝
    async batchRefuseComments() {
            const { ctx, app } = this;
            const { batchList } = ctx.request.body;
            batchList.forEach((item) => {
                app.mysql.update("comments", {
                    id: item.id,
                    state: 0,
                });
            });
            return "操作成功";
        }
        //*************用户端******************* */
        //获取每条文章评论
    async getArticleComment() {
        const { ctx, app } = this;
        const { id } = ctx.params;
        const comment = await app.mysql.select("comments");
        let result = [];
        comment.forEach((item) => {
            if (item.state * 1) {
                result.push(item);
            }
        });
        result = result.filter((item) => item.article_id * 1 === id * 1);
        return result;
    }
}
module.exports = CommentService;